<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>随机点名</title>
    <style>
        #content {
            width: 600px;
            height: 400px;
            background: #000;
            margin: 100px auto;
            text-align: center;
            position: relative;
            line-height: 300px;
            color: dodgerblue;
            font-size: 70px;
        }

        #btn1 {
            background: #ccc;
            width: 180px;
            height: 80px;
            font-size: 30px;
            color: #f40;
            border-radius: 12px;
            position: absolute;
            bottom: 30px;
            left: 50%;
            margin-left: -90px;

        }
    </style>
</head>

<body>
    <div id="content">
        <span id="span1">
            点击开始
        </span>
        <button id="btn1">
            start
        </button>

    </div>

    <script>
        // 点击start后按钮本身文字变成end ，span1中可以随机显示某一个名字，点击end 可以按钮本身文字变为开始 span1中显示具体名字
        var arr = ["刘备", "关羽", "张飞", "赵云", "马超", "百里守约", "牛魔", "吕布"];
        var button = document.querySelector("button");
        var bg = document.querySelector("#span1");
        // var i = 0;
        var timer;
        var flag = true;
        button.onclick = function () {
            if (flag) {
                button.innerText = "end"
                timer = setInterval(function () {
                    var suiji = Math.round(Math.random() * (arr.length - 1));
                    bg.innerText = (arr[suiji]);
                }, 100)
                flag = !flag;
            } else {
                clearInterval(timer);
                flag = true;
                button.innerText = "start"
            }
        }
    </script>
</body>

</html>